package com.tanhua.manage.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.mapper.OprationLogMapper;
import com.tanhua.manage.pojo.OprationLog;
import com.tanhua.manage.pojo.PublishInfo;
import com.tanhua.manage.vo.OprationLogVo;
import com.tanhua.manage.vo.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OprationService  extends ServiceImpl<OprationLogMapper,OprationLog> {
    @Autowired
    private OprationLogMapper oprationLogMapper;

    public Pager<OprationLogVo> findLogByPage(Integer page, Integer pageSize, String userName, String operationMethod, String sd,String ed) {
        // 日期格式转换
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Long sdTime = null;
        Long edTime = null;
        try {
            if(StrUtil.isNotEmpty(sd)){
                sdTime = df.parse(sd).getTime();
            }if(StrUtil.isNotEmpty(ed)){
                edTime = df.parse(ed).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        IPage<OprationLog> oprationLogIPage = new Page<>(page,pageSize);
        Wrapper<OprationLog> queryWrapper = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(userName)){
            ((QueryWrapper<OprationLog>) queryWrapper).like("user_name",userName);
        }
        if(StrUtil.isNotEmpty(operationMethod)){
            ((QueryWrapper<OprationLog>) queryWrapper).like("opration_describe",operationMethod);
        }
        if(sdTime != null && edTime != null && sdTime != -1 && sdTime.equals(edTime)){
            // 当 开始时间 和 结束时间 为相同时，即同一天。结束时间+1天毫秒数
            edTime = edTime + 86400000;
            ((QueryWrapper<OprationLog>) queryWrapper).between("create_date",sdTime,edTime);
        }
        if(sdTime != null && sdTime > 0){
            ((QueryWrapper<OprationLog>) queryWrapper).gt("create_date",sdTime);
        }
        if(edTime != null && edTime > 0){
            ((QueryWrapper<OprationLog>) queryWrapper).lt("create_date",edTime);
        }
        ((QueryWrapper<OprationLog>) queryWrapper).orderByDesc("created");
        //List<OprationLog> oprationLogs = this.oprationLogMapper.selectList(queryWrapper);
        this.oprationLogMapper.selectPage(oprationLogIPage, queryWrapper);
        // 封装返回前台
        List<OprationLogVo> oprationLogVos = new ArrayList<>();
        List<OprationLog> records = oprationLogIPage.getRecords();
        if(records.size()>0){
            for (OprationLog record : records) {
                OprationLogVo oprationLogVo = new OprationLogVo();
                oprationLogVo.setId(record.getId());
                oprationLogVo.setUserName(record.getUserName());
                oprationLogVo.setOprationIp(record.getOprationIp());
                oprationLogVo.setOprationTime(record.getCreated());
                oprationLogVo.setOprationDescribe(record.getOprationDescribe());
                oprationLogVo.setOprationMessage(record.getOprationMessage());
                oprationLogVos.add(oprationLogVo);
            }
        }

        return new Pager<OprationLogVo>(oprationLogIPage.getTotal(),
                oprationLogIPage.getSize(),
                oprationLogIPage.getPages(),page,oprationLogVos
                );

    }

    public String findOprationDetails(Integer id) {
        Wrapper<OprationLog> queryWrapper = new QueryWrapper<>();
        ((QueryWrapper<OprationLog>) queryWrapper).eq("id",id);
        OprationLog oprationLog = this.oprationLogMapper.selectOne(queryWrapper);
        if(oprationLog == null ){
            return "操作记录id不存在";
        }
        return oprationLog.getLogDetails();
    }
}
